
#include <jrtplib3/rtpsession.h>
#include <jrtplib3/rtpsessionparams.h>
#include <jrtplib3/rtpudpv4transmitter.h>
#include <jrtplib3/rtppacket.h>

#include <stdio.h>
#include <iostream>

using namespace jrtplib;
using namespace std;

void checkerror(int rtperr)
{
	if (rtperr < 0)
	{
		std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
		exit(-1);
	}
}

int main(void)
{
	RTPSession sess;
	std::string ipstr = "127.0.0.1";
	uint16_t destport = 11554;
	uint16_t portbase = 11556;
	int num = 100; //发送100个包

	cout << "Using JRTPLIB version;" << RTPLibraryVersion::GetVersion().GetVersionString() << endl;
	cout << "Enter local portbase:" << portbase << endl;
	cout << "Enter  portbase:" << portbase << endl;

	uint32_t destip = inet_addr(ipstr.c_str());

	if (destip == INADDR_NONE)
	{
		cerr << "Bad IP address specified" << endl;
		return -1;
	}

	destip = ntohl(destip);

	cout << "创建Session" << endl;
	RTPUDPv4TransmissionParams transparams;
	transparams.SetPortbase(portbase);
	RTPSessionParams sessparams;
	sessparams.SetOwnTimestampUnit(1.0 / 10.0);
	sessparams.SetAcceptOwnPackets(true);
	int status = sess.Create(sessparams, &transparams);
	checkerror(status);
	cout << "添加地址" << endl;
	RTPIPv4Address addr(destip,destport);
    status = sess.AddDestination(addr);
	checkerror(status);

	for (int i = 1 ; i <= num ; i++)
    {
        printf("\nSending packet %d/%d\n",i,num);
        status = sess.SendPacket((void *)"1234567890",10,0,false,10);
        checkerror(status);
        RTPTime::Wait(RTPTime(1,0));
    }
    sess.BYEDestroy(RTPTime(10,0),0,0);
	printf("Hello World\n");
	return 0;
}